<?php
header("Content-Type: text/html");
header("charset: utf-8");
include_once("_common.php");

/*areaName이 있으면 좌표를 검색*/
$stx = get_text(stripslashes($stx));
$areaName = get_text(stripslashes($stx));

if ($areaName){
	$xml_parser = @xml_parser_create() or die("XML 파서를 생성하지 못했습니다.");

	$xml = file_get_contents("http://maps.google.co.kr/maps/geo?key=ABQIAAAAMzk_wa5vWXjb8Hlx2Zsp0RTnHXbg4JNqWsj05gtbf8SYI4YLQxTRK3ZParLpK6mDgyRozTiW0R4E6Q&output=xml&q=".urlencode($areaName));
	
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE);
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, TRUE);
	
	// XML 문서를 파싱하여 그 결과를 두 배열에 저장한다.
	xml_parse_into_struct($xml_parser, $xml, $value, $index);
	
	
	xml_parser_free($xml_parser);
	
	$geoArr = explode(",", $value[$index["coordinates"][0]][value]);

	$longitude = $geoArr[0];
	$latitude = $geoArr[1];
	
	if (!$longitude){
		$longitude = 127.0363890;
		$latitude = 37.5006350;
	}
}else{ //없으면 역삼역으로 검색
	$longitude = 127.0363890;
	$latitude = 37.5006350;
}
/********areaName이 있으면 좌표를 검색 끝****/


$sop = strtolower($sop);
if ($sop != "and" && $sop != "or")
    $sop = "and";

// 분류 선택 또는 검색어가 있다면
$stx = urldecode(trim($stx));

$board = sql_fetch(" select * from g4_board where bo_table = '$bo_table' ");

if ($sca || $stx || $bo_gubun)
{
    if($bo_gubun == "my_write"){
    	//내게시물의 글일경우 검색조건 추가 -> outlogin.skin.2.php
    	$sql_search = " mb_id = '$mb_id' and wr_is_comment = 0 ";
    } else if($bo_gubun == "member_write"){
    	//회원게시물의 글일경우 검색조건 추가 -> outlogin.skin.2.php
    	$sql_search = " mb_id = '$param_data' and wr_is_comment = 0 ";
    } else if($bo_gubun == "my_interest"){
    	//내관심상품의 글일경우 검색조건 추가 -> outlogin.skin.2.php
    	$sql_search = " wr_id in (select wr_id from g4_scrap where bo_table = '$write_table' and mb_id = '$mb_id') ";
    } else if($bo_gubun == "my_sellgubun"){
    	//매매구분에 따른 검색조건 추가
    	$sql_search = " wr_1 = '".$param_data."' ";
    } else{
	    /*$sql_search = get_sql_search($sca, $sfl, $stx, $sop);
	
	    // 가장 작은 번호를 얻어서 변수에 저장 (하단의 페이징에서 사용)
	    $sql = " select MIN(wr_num) as min_wr_num from $write_table ";
	    $row = sql_fetch($sql);
	    $min_spt = $row[min_wr_num];
	
	    if (!$spt) $spt = $min_spt;
	
	    $sql_search .= " and (wr_num between '".$spt."' and '".($spt + $config[cf_search_part])."') ";*/
	}
	// 원글만 얻는다. (코멘트의 내용도 검색하기 위함)
    if ($sql_search)
    	$sql = " select distinct wr_parent from $write_table where $sql_search ";
    else
    	$sql = " select distinct wr_parent from $write_table ";
	    
    $result = sql_query($sql);
	$total_count = mysql_num_rows($result);
	
	$total_count = $board['bo_count_write'];
}
else
{
    $sql_search = "";

    $total_count = $board['bo_count_write'];
}

$total_page  = ceil($total_count / $page_row);  // 전체 페이지 계산
if (!$page) { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
//$from_record = ($page - 1) * $page_row; // 시작 열을 구함
$from_record = $board['bo_page_rows'] + ($page - 2) * $page_row; // 시작 열을 구함

// 정렬에 사용하는 QUERY_STRING
$qstr2 = "bo_table=g4_board&sop=$sop";

if (!$sst)
{
    if ($board['bo_sort_field'])
        $sst = $board['bo_sort_field'];
    else
        $sst  = "wr_num, wr_reply";
    $sod = "";
}
else {
    // 게시물 리스트의 정렬 대상 필드가 아니라면 공백으로 (nasca 님 09.06.16)
    // 리스트에서 다른 필드로 정렬을 하려면 아래의 코드에 해당 필드를 추가하세요.
    // $sst = preg_match("/^(wr_subject|wr_datetime|wr_hit|wr_good|wr_nogood)$/i", $sst) ? $sst : "";
    $sst = preg_match("/^(wr_datetime|wr_hit|wr_good|wr_nogood)$/i", $sst) ? $sst : "";
}

if ($sst)
    //$sql_order = " order by $sst $sod ";
    
	//구분이 있으면 최신순 
    $sql_order = " order by ".($gubun?"":"abs(a.latitude - {$latitude} + a.longitude - {$longitude}),")."$sst $sod ";

if ($sca || $stx || $bo_gubun)
{
    if ($sql_search)
    	$sql = " select distinct wr_parent from $write_table a where $sql_search $sql_order limit $from_record, $board[bo_page_rows] ";
    else
    	$sql = " select distinct wr_parent from $write_table a  $sql_order limit $from_record, $board[bo_page_rows] ";
}
else
{
    $sql = " select *, CASE WHEN TIMESTAMPDIFF(MINUTE, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(SECOND, wr_datetime, now()),'초전') WHEN TIMESTAMPDIFF(HOUR, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(MINUTE, wr_datetime, now()),'분전') WHEN TIMESTAMPDIFF(DAY, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(HOUR, wr_datetime, now()),'시간전') ELSE concat(TIMESTAMPDIFF(DAY, wr_datetime, now()),'일전') END diff from $write_table a where wr_is_comment = 0 $sql_order limit $from_record, $page_row ";
}
//echo $sql;
$result = sql_query($sql);

// 년도 2자리
$today2 = $g4[time_ymd];

$list = array();
$i = 0;

if (!$sca && !$stx && !$bo_gubun)
{
    $arr_notice = explode("\n", trim($board['bo_notice']));
    for ($k=0; $k<count($arr_notice); $k++)
    {
        if (trim($arr_notice[$k])=='') continue;

        $row = sql_fetch(" select *, CASE WHEN TIMESTAMPDIFF(MINUTE, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(SECOND, wr_datetime, now()),'초전') WHEN TIMESTAMPDIFF(HOUR, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(MINUTE, wr_datetime, now()),'분전') WHEN TIMESTAMPDIFF(DAY, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(HOUR, wr_datetime, now()),'시간전') ELSE concat(TIMESTAMPDIFF(DAY, wr_datetime, now()),'일전') END diff from $write_table where wr_id = '$arr_notice[$k]' ");

        if (!$row[wr_id]) continue;

        $list[$i] = get_list($row, $board, $board_skin_path, $board['bo_subject_len']);
        $list[$i][is_notice] = true;
		
        $i++;
    }
}

$k = 0;

while ($row = sql_fetch_array($result))
{
    // 검색일 경우 wr_id만 얻었으므로 다시 한행을 얻는다
    if ($sca || $stx || $bo_gubun)
        $row = sql_fetch(" select *, CASE WHEN TIMESTAMPDIFF(MINUTE, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(SECOND, wr_datetime, now()),'초전') WHEN TIMESTAMPDIFF(HOUR, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(MINUTE, wr_datetime, now()),'분전') WHEN TIMESTAMPDIFF(DAY, wr_datetime, now()) < 1 THEN concat(TIMESTAMPDIFF(HOUR, wr_datetime, now()),'시간전') ELSE concat(TIMESTAMPDIFF(DAY, wr_datetime, now()),'일전') END diff from $write_table where wr_id = '$row[wr_parent]' ");

    $list[$i] = get_list($row, $board, $board_skin_path, $board['bo_subject_len']);
    if (strstr($sfl, "subject"))
        $list[$i][subject] = search_font($stx, $list[$i][subject]);
    $list[$i][is_notice] = false;
    //$list[$i][num] = number_format($total_count - ($page - 1) * $board[bo_page_rows] - $k);
    $list[$i][num] = $total_count - ($page - 1) * $board['bo_page_rows'] - $k;

    $i++;
    $k++;
}

if(count($list) < 1){
	echo "0";
}else{
	if($write_table == "g4_write_sellhome"){
		for ($i=0; $i<count($list); $i++) {
			$bg = $i%2 ? 0 : 1;
			
			echo "\n		<div class=\"home_info\">";
			echo "\n			<div style=\"overflow:hidden;margin-bottom:5px;\">";
			echo "\n				<div class=\"text01\" style=\"float:left;\">";
			echo "\n					<a href=\"javascript:mapCenter(".$list[$i][latitude].", ".$list[$i][longitude].");\">".$list[$i][mb_addr1]."</a>";
			echo "\n					<img src=\"../img/home/subway_3.gif\" style=\"vertical-align:middle;\" />";
			echo "\n				</div>";
			echo "\n				<div style=\"text-align:right;float:right;\">";
			echo "\n					By ".$list[$i][name];
			echo "\n					<div style=\"width:56px; height:19px; background:url('../img/product/days02_bg.gif') no-repeat; color:#fff; font-size:12px;text-align:center;padding-top:3px;\">";
			echo "\n						".$list[$i][diff];
			echo "\n					</div>";
			echo "\n				</div>";
			echo "\n			</div>";
			echo "\n			<div class=\"mm_info01\">";
			echo "\n				<div>";
			if(!$list[$i][file][0]){
				echo "\n				<img src=\"/img/noImage183x136.gif\" style=\"vertical-align:middle;border:1px solid #d5d5d5;\" width=\"120\" height=\"100\" class='bigImg'/>";
			}else{
				echo "\n				<img src=\"".$list[$i][file][0][path]."/".$list[$i][file][0][file]."\" style=\"vertical-align:middle;border:1px solid #d5d5d5;\" width=\"120\" height=\"100\" class='bigImg'/>";
			}
			echo "\n				</div>";
			echo "\n				<div>";
			if ($list[$i][file][0]){
				echo "\n				<a href=\"javascript:imgChange(".$i.",'".$list[$i][file][0][path]."/".$list[$i][file][0][file]."');\"><img src='".$list[$i][file][0][path]."/".$list[$i][file][0][file]."' align='absmiddle' border='0' width='30' height='25'></a>";
			}else{
				echo "\n				<img src=\"/img/noImage183x136.gif\" width=\"30\" height=\"25\" />";
			}
			if($list[$i][file][1]){
			 	echo "\n				<a href=\"javascript:imgChange(".$i.",'".$list[$i][file][1][path]."/".$list[$i][file][1][file]."');\"><img src='".$list[$i][file][1][path]."/".$list[$i][file][1][file]."' align='absmiddle' border='0' width='30' height='25'></a>";
			}else{
				echo "\n				<img src=\"/img/noImage183x136.gif\" width=\"30\" height=\"25\" />";
			}
			if($list[$i][file][2]){
				echo "\n				<a href=\"javascript:imgChange(".$i.",'".$list[$i][file][2][path]."/".$list[$i][file][2][file]."');\"><img src='".$list[$i][file][2][path]."/".$list[$i][file][2][file]."' align='absmiddle' border='0' width='30' height='25'></a>";
			}else{
				echo "\n				<img src=\"/img/noImage183x136.gif\" width=\"30\" height=\"25\" />";
			}
			if($list[$i][file][3]){
				echo "\n				<a href=\"javascript:imgChange(".$i.",'".$list[$i][file][3][path]."/".$list[$i][file][3][file]."');\"><img src='".$list[$i][file][3][path]."/".$list[$i][file][3][file]."' align='absmiddle' border='0' width='30' height='25'></a>";
			}else{
				echo "\n				<img src=\"/img/noImage183x136.gif\" width=\"30\" height=\"25\" />";
			}
			echo "\n				</div>";
			echo "\n			</div>";
			echo "\n			<div class=\"mm_info02\">";
			echo "\n				<div style=\"color:#464646; letter-spacing:-0.05em;margin-bottom:20px;\">";
			if($list[$i][wr_2]=="L1"){
				echo "\n				원룸";
			}else if($list[$i][wr_2]=="L2"){
				echo "\n				오피스텔";
			}else if($list[$i][wr_2]=="L3"){
				echo "\n				아파트";
			}
			echo "\n					&nbsp;&nbsp;".$list[$i][wr_3]."&nbsp;&nbsp;";
			if($list[$i][wr_1]=='D1'){
				echo "\n				<img src=\"../img/home/b_001.jpg\" style=\"vertical-align:middle;\" />";
			}else if($list[$i][wr_1]=='D2'){
				echo "\n				<img src=\"../img/home/b_002.jpg\" style=\"vertical-align:middle;\" />";
			}else if($list[$i][wr_1]=='D3'){
				echo "\n				<img src=\"../img/home/b_003.jpg\" style=\"vertical-align:middle;\" />";
			}
			echo "\n					".han_price($list[$i][homePrice]);
			echo "\n				</div>";
			echo "\n				<p style=\"padding-bottom:13px;\"><!-- img src=\"../img/home/icon_pro.gif\" style=\"vertical-align:middle;\" /--></p>";
			echo "\n				<p style=\"color:#929292;margin-bottom:14px;\">입주가능".$list[$i][wr_4]."</p>";
			echo "\n				<p style=\"padding-bottom:13px;text-align:right;\">";
			if($mb_id){
				echo "\n				<img src=\"../img/home/btn_pomm.gif\" style=\"vertical-align:middle;cursor:pointer;\" onclick=\"javascript:goScrap(".$list[$i][wr_id].");\" />";
			}
			echo "\n				</p>";
			if($list[$i][chkMore]){
				echo "\n			<p style=\"padding-bottom:5px;\"><img src=\"../img/product/icon_select01.gif\" style=\"vertical-align:middle;\" /></p>";
			}
			echo "\n			</div>";
			echo "\n		</div>";
		}
	}else{
		for ($i=0; $i<count($list); $i++) {
			$bg = $i%2 ? 0 : 1;
			
			echo "\n		<div class=\"home_info\">";
			echo "\n			<div class=\"mm_info03\">";
			echo "\n				<div style=\"text-align:right;margin-bottom:18px;\">By ".$list[$i][name];
			echo "\n					<div style=\"width:56px; height:19px; background:url('../img/product/days02_bg.gif') no-repeat; color:#fff; font-size:12px;text-align:center;padding-top:3px;\">";
			echo "\n						".$list[$i][diff];
			echo "\n					</div>";
			echo "\n				</div>";
			echo "\n				<div class=\"hope_info\">";
			echo "\n					<table width=\"100%\">";
			echo "\n						<tr>";
			echo "\n							<td><img src=\"../img/home/stit_zone.gif\" style=\"vertical-align:middle;\" /></td>";
			echo "\n							<td><img src=\"../img/home/stit_in.gif\" style=\"vertical-align:middle;\" /></td>";
			echo "\n							<td><img src=\"../img/home/siti_pay.gif\" style=\"vertical-align:middle;\" /></td>";
			echo "\n						</tr>";
			echo "\n						<tr>";
			echo "\n							<td>".$list[$i][mb_addr1]."</td>";
			echo "\n							<td>".$list[$i][wr_4]."</td>";
			echo "\n							<td>";
			if($list[$i][wr_1]=='D1'){
				echo "\n							<img src=\"../img/home/b_001.jpg\" style=\"vertical-align:middle;\" />";
			}else if ($list[$i][wr_1]=='D2'){
				echo "\n							<img src=\"../img/home/b_002.jpg\" style=\"vertical-align:middle;\" />";
			}else if ($list[$i][wr_1]=='D3'){
				echo "\n							<img src=\"../img/home/b_003.jpg\" style=\"vertical-align:middle;\" />";
			}
			echo "\n								".han_price($list[$i][homePrice]);
			echo "\n							</td>";
			echo "\n						</tr>";
			echo "\n					</table>";
			echo "\n				</div>";
			echo "\n				<div style=\"color:#999; letter-spacing:-0.05em;margin-bottom:26px;\">";
			echo "\n					".$list[$i][wr_content];
			echo "\n				</div>";
			echo "\n				<table width=\"100%\" style=\"margin-bottom:15px;\">";
			echo "\n					<tr>";
			echo "\n						<td valign=\"top\" align=\"right\">";
			echo "\n							<a href=\"javascript:;\" onclick=\"sSNS('twitter', '', '".$list[$i][wr_content]."');\"><img src=\"../img/home/btn_tweeter.gif\" style=\"vertical-align:middle;\" /></a>";
			echo "\n							<a href=\"javascript:;\" onclick=\"sSNS('me2day', '', '".$list[$i][wr_content]."');\"><img src=\"../img/home/btn_me2day.gif\" style=\"vertical-align:middle;\" /></a>";
			echo "\n							<a href=\"javascript:;\" onclick=\"sSNS('facebook', '', '".$list[$i][wr_content]."');\"><img src=\"../img/home/btn_facebook.gif\" style=\"vertical-align:middle;\" /></a>";
			echo "\n							<a href=\"javascript:;\" onclick=\"sSNS('cyworld', '', '".$list[$i][wr_content]."');\"><img src=\"../img/home/btn_cyworld.gif\" style=\"vertical-align:middle;\" /></a>";
			echo "\n						</td>";
			echo "\n						<td width=\"100\" align=\"right\">";
			if($mb_id){
				echo "\n						<img src=\"../img/home/btn_pomm.gif\" style=\"vertical-align:middle;cursor:pointer;\" onclick=\"javascript:goScrap(".$list[$i][wr_id].");\" />";
			}
			echo "\n							<p style=\"color:#999; padding-top:5px;\">댓글(0)</p>";
			echo "\n						</td>";
			echo "\n					</tr>";
			echo "\n				</table>";
			echo "\n			</div>";
			echo "\n		</div>";
		}
	}
}

?>